﻿using System.IO;
using UtilityToolsCollect.SerializeToolUtility;

namespace UtilityToolsCollect.ObjectsLibrary.Configs
{
    public abstract class XmlConfigurationAbstract<T> : ConfigurationAbstract<T>, IConfiguration<T> where T : ConfigurationBase, new()
    {
        public static T Current
        {
            get
            {
                if (m_Current == DefaultEmpty)
                {
                    Load();
                    m_Current?.Loaded();
                }
                return m_Current!;
            }
            set
            {
                if (value == m_Current)
                    return;
                m_Current = value;
            }
        }

        public static void Load() { if (File.Exists(ConfigurationFilePath)) m_Current = XmlSerialize.FileAndPathTool.PathToXmlDeserialize<T>(ConfigurationFilePath); m_Current?.Sort(); }
        public static void Save() { m_Current?.Sort(); XmlSerialize.FileAndPathTool.XmlSerializerToXmlFile(ConfigurationFilePath, Current, true); }

    }
}